import 'package:floor/floor.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/generated/locales.g.dart';

@Entity(tableName: 'spo2', indices: [
  Index(value: ['calendar', 'address'])
])
class Spo2Model extends BaseModel {
  @ColumnInfo(name: 'calendar_time')
  late String? calendarTime;
  late String? calendar;
  late int? time;
  @ColumnInfo(name: 'oxygen_value')
  late int? oxygenValue;

  Spo2Model({
    super.id,
    super.address,
    super.dateCreated,
    super.dateModified,
    this.calendarTime,
    this.calendar,
    this.time,
    this.oxygenValue,
  });

  @override
  Map<String, dynamic> toMap() {
    return {
      'calendarTime': calendarTime,
      'calendar': calendar,
      'oxygenValue': oxygenValue,
    };
  }

  @override
  bool from(Map map) {
    // TODO: implement from
    throw UnimplementedError();
  }

  ///
  get spo2Summary => Spo2Summary(oxygenValue: oxygenValue ??= 0);

  @override
  List<Object?> get props => [];

// @override
// bool? get stringify => true;
}

class Spo2Summary {
  late int oxygenValue;
  late List<int> oxygenValues = [];
  List<String> calendarTimes = [];
  late List<Spo2Summary> oxygenAvgs = [];
  String calendar = '';
  String calendarTime = '';

  Spo2Summary.from(Spo2Model model) {
    oxygenValue = model.oxygenValue ?? 0;
    calendar = model.calendar ?? '';
    calendarTime = model.calendarTime ?? '';
  }

  Spo2Summary({this.calendar = '', this.calendarTime = '', this.oxygenValue = 0});

  /// 获取当前血氧，在首页小组件显示
  get spo2Text => oxygenValue == 0 ? LocaleKeys.no_records_text.ts : '$oxygenValue%';

  /// 血氧折线图上的tooltip
  get tooltipText {
    String ov = oxygenValue == 0 ? '--' : '$oxygenValue%';
    return '$ov \n $calendarTime';
  }
}
